package my_web.service;
import java.util.ArrayList;
import java.util.List;
import dao.dao.impl.DaoFactory;
import my_web.Interface.MissionInterface;
import my_web.bean.Emp;
import my_web.bean.Mission;
import my_web.model.EmpDto;
import my_web.model.MissionDto;
import my_web.page.Page;
import my_web.page.Imp.PageEngineFactory;
public class MissionService implements MissionInterface {
	public  Page DynamicQueryAllMissions(MissionDto dto, Emp emp) {
		StringBuilder where=new StringBuilder();
		List<Object> paramList=new ArrayList<>();
		String sql = null;
		//sql="select M.*,E.e_name from mission_table M left outer join Emp_table E on M.e_id=E.e_id where 1=1";
		//String s = " and m.d_id=" + emp.getD_id();
		sql="select M.*,E.e_name,E.d_id from mission_table M left outer join Emp_table E on M.e_id=E.e_id where E.d_id=?";
		paramList.add(0,emp.getD_id());
		if(dto!=null){
			if(dto.getM_id()!=null && !dto.getM_id().equals("")){
				where.append(" and m_id like ?");
				paramList.add("%"+dto.getM_id()+"%");
			}
			if(dto.getM_name()!=null && !dto.getM_name().equals("")){
				where.append(" and m_name like ?");
				paramList.add("%"+dto.getM_name()+"%");
			}
			if(dto.getE_id()!=null && !dto.getE_id().equals("")){
				where.append(" and e_id like ?");
				paramList.add("%"+dto.getE_id()+"%");
			}
			if(dto.getM_content()!=null && !dto.getM_content().equals("")){
				where.append(" and m_content like ?");
				paramList.add("%"+dto.getM_content()+"%");
			}
			if(dto.getM_start_date()!=null && !dto.getM_start_date().equals("")){
				where.append(" and m_start_date>=?");
				paramList.add(dto.getM_start_date());
			}
			if(dto.getM_end_date()!=null && !dto.getM_end_date().equals("")){
				where.append(" and m_end_date<=?");
				paramList.add(dto.getM_end_date());
			}
			if(dto.getM_state()!=null && !dto.getM_state().equals("")){
				where.append(" and m_state like ?");
				paramList.add("%"+dto.getM_state()+"%");
			}
		}
		where=where.append("order by m_id");
		try{
			return PageEngineFactory.createOraclePage(Mission.class, DaoFactory.getCurrDao(), sql + where, dto, paramList.toArray());
		}catch(RuntimeException e){
			throw e;
		}
	}
	@Override
	public void addMission(MissionDto dto) {
		DaoFactory.getCurrDao().update("insert into mission_table"
				+ "(m_id,m_name,e_id,m_content,m_start_date,m_end_date,m_state)values(?,?,?,?,?,?,?)", 
				dto.getM_id(),dto.getM_name(),dto.getE_id(),dto.getM_content(),dto.getM_start_date(),dto.getM_end_date(),dto.getM_state());
	}
	@Override
	public void deleteMissionById(String m_id) {
		DaoFactory.getCurrDao().update("delete from mission_table where m_id=?",m_id);
	}
	@Override
	public MissionDto getMissionById(String m_id) {
		return DaoFactory.getCurrDao().queryBean(MissionDto.class, "select *from mission_table where m_id=?", m_id);
	}
	@Override
	public void updateMission(MissionDto dto) {
		DaoFactory.getCurrDao().update("update mission_table set m_name=?,m_content=?,m_end_date=? where m_id=?", dto.getM_name(),dto.getM_content(),dto.getM_end_date(),dto.getM_id());
	}
	@Override
	public List<Emp> getEmpById(String e_id) {
		return DaoFactory.getCurrDao().queryBeanList(Emp.class, "select *from Emp_table where e_id=?", e_id);
	}
	@Override
	public MissionDto getDeliverMissionById(String m_id) {
		return DaoFactory.getCurrDao().queryBean(MissionDto.class,"select *from mission_table where m_id=?", m_id);
	}
	@Override
	public void DoDeliver(String s1, String s2) {
		DaoFactory.getCurrDao().update("update mission_table set m_state=? where m_id=?","已发布",s2);
		DaoFactory.getCurrDao().update("insert into deliver_table(m_id,e_id,d_state)values(?,?,?)",s2,s1,"下发");
	}
	@Override
	public List<Emp> getAllEmp(Emp emp) {
		return DaoFactory.getCurrDao().queryBeanList(
						Emp.class, "select E.*,D.d_name,P.p_name from Emp_table E left join Dep_table D on D.d_id=E.d_id left join Pos_table P on P.p_id=E.p_id  where E.p_id=?and E.d_id=?","P003",emp.getD_id());
	}
	@Override
	public MissionDto getMission(String m_id) {
		return DaoFactory.getCurrDao().queryBean(MissionDto.class, "select * from mission_table where m_id=?",m_id);
	}
	@Override
	public Page getAllMissionEmp(EmpDto dto, String d_id) {
		return (Page) DaoFactory.getCurrDao().queryBeanList(Emp.class, "select *from Emp_table where d_id=?",d_id);
	}
	@Override
	public List<Emp> getEmp(Emp emp) {//根据部门d_id 查询本部门的所有员工
		return DaoFactory.getCurrDao().queryBeanList(Emp.class, "select *from Emp_table where d_id=?",emp.getD_id());
	}
	@Override
	public Page getAllMissionEmp(EmpDto dto, Emp emp) {
		List list = new ArrayList();
		list.add(emp.getD_id());
		return new PageEngineFactory().createOraclePage(EmpDto.class, DaoFactory.getCurrDao(), "select *from Emp_table where p_id='P003' and d_id=?", dto, list.toArray());
	}
	@Override
	public Object getMissionInfoById(String m_id) {
		return DaoFactory.getCurrDao().query("select D.*,M.m_id, M.m_name, M.m_content, M.m_end_date,E.e_name,E.d_id from deliver_table D left join mission_table M on D.m_id=M.m_id left join Emp_table E on D.e_id=E.e_id where D.m_id=?",m_id);
	}
}